package com.navi.core.util;

import org.apache.log4j.Logger;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

public class XxdUtils {
    private static Logger logger = Logger.getLogger(XxdUtils.class);
    private static String strMySQLTimeFormat = "yyyy-MM-dd HH:mm:ss";
    private static char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
            'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S',
            'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    public static String stackTraceToString(Exception excp) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        excp.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

    public static String getEvtTime() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String evt_timestamp = tempDate.format(now.getTime());
        return evt_timestamp;
    }

    public static Timestamp getCurrentTimestamp() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String evt_timestamp = tempDate.format(now.getTime());
        Timestamp timestatmp = null;
        try {
            timestatmp = Timestamp.valueOf(evt_timestamp);
        } catch (Exception ex) {
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        return timestatmp;
    }

    public static String getEvtTimeforVer() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
        String evt_timestamp = tempDate.format(now.getTime());
        return evt_timestamp;
    }

    public static String getRvTime() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String evt_timestamp = tempDate.format(now.getTime());
        return evt_timestamp;
    }

    public static String getMillisecondTime() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String evt_timestamp = tempDate.format(now.getTime());
        return evt_timestamp;
    }

    public static String getMillTimeStr() {
        Calendar now = Calendar.getInstance();
        // SimpleDateFormat tempDate = new
        // SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
        String evt_timestamp = tempDate.format(now.getTime());
        return evt_timestamp;
    }

    public static Date getMillTime() {// TODO Need format
        return String2Date(getMillTimeStr());
    }

    public static String formatEvtTime(Calendar calendar) {
        SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
        String evt_timestamp = tempDate.format(calendar.getTime());
        return evt_timestamp;
    }

    public static String formatRvTime(Calendar calendar) {
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String evt_timestamp = tempDate.format(calendar.getTime());
        return evt_timestamp;
    }

    public static Date String2Date(String dateString) {
        Date date = new Date();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(strMySQLTimeFormat);
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }

    public static Date String2Time(String timeString) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH.mm.ss");
            date = sdf.parse(timeString);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }

    public static Timestamp String2Timestamp(String timeString) {
        Timestamp timestatmp = null;
        try {
            timestatmp = Timestamp.valueOf(timeString);
        } catch (Exception ex) {
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        return timestatmp;
    }

    /**
     * Get time diff value (By min)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getDiffMin(String startTime, String endTime) {
        long diff;
        long diffMin = 0;
        long nm = 1000 * 60;// 一分钟的毫秒数
        SimpleDateFormat sd = new SimpleDateFormat(strMySQLTimeFormat);
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            diffMin = diff / nm;// 计算差多少分钟
        } catch (ParseException ex) {
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        return diffMin;
    }

    /**
     * Get time diff value (By min)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getDiffMin(Timestamp startTime, Timestamp endTime) {
        long diffMin = 0;
        long nm = 1000 * 60;// 一分钟的毫秒数

        long diff = endTime.getTime() - startTime.getTime();
        diffMin = diff / nm;// 计算差多少分钟 //TODO Double

        return diffMin;
    }

    public static long getDiffSec(Timestamp startTime, Timestamp endTime) {
        long diffSec = 0;
        long nm = 1000;//

        long diff = endTime.getTime() - startTime.getTime();
        diffSec = diff / nm;// 计算差多少秒 //TODO Double
        return diffSec;
    }

    public static boolean isSpaceCheck(String str) {
        if (null == str || str.length() <= 0 || ("").equals(str)) {
            return true;
        }
        return false;
    }

    /*****
     * 获取 double类型时间 逻辑混乱，不建议深入研究
     * ******/
    public static Double getDoubleTime() {
        long now = System.currentTimeMillis(); // 微秒级当前时间
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(now);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DATE);
        // int year = 2013;
        // int month = 10;
        // int day = 27;
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        int millisecond = c.get(Calendar.MILLISECOND);
        // System.out.println("当前微秒级时间:"+System.currentTimeMillis());
        // System.out.println("当前时间: " + c.get(Calendar.YEAR) + "年 "
        // + (c.get(Calendar.MONTH) + 1) + "月 " + c.get(Calendar.DATE)
        // + "日  周" + (c.get(Calendar.DAY_OF_WEEK) - 1) + "  "
        // + c.get(Calendar.HOUR) + "时 " + c.get(Calendar.MINUTE) + "分 "
        // + c.get(Calendar.SECOND) + "秒 " + c.get(Calendar.MILLISECOND)
        // + " 微秒");
        Double days = Double.valueOf("0");// 统计天数
        for (int i = 1900; i < year; i++) {
            days += getDaysByYear(i);
        }
        // System.out.println("passDays :" + days + "\n");
        int[] days_each_month = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30,
                31, 30, 31 };
        if (isLeepYear(year)) {
            days_each_month[1] = 29;
        }

        for (int i = 0; i < month - 1; i++) {
            days += days_each_month[i];
        }

        days = days + day + 1;
        // 获取微秒
        // long nanoTime = System.nanoTime();
        // String snonoTime = nanoTime+"";
        // String sMicroSecond = snonoTime.substring(snonoTime.length()-6,
        // snonoTime.length()-3);

        double seconds = hour * 3600 + minute * 60 + second;
        double millSeconds = millisecond + seconds * 1000;// 000
        // +Double.valueOf(sMicroSecond);

        Double d = Double.valueOf(millSeconds / (3600 * 24 * 1000) + "");
        return Double.valueOf(days + d);
    }

    // 获得某年的天数
    private static int getDaysByYear(int year) {
        if (isLeepYear(year)) {
            return 366;
        } else {
            return 365;
        }
    }

    // 判断是否是闰年
    private static boolean isLeepYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    public static String comInt2String(int value, int length) {
        // System.out.println("value:" + value);
        // System.out.println("length:" + length);
        String s = String.valueOf(value);
        if (s.length() > length) {
            throw new RuntimeException("length is too short.");
        } else if (s.length() == length) {
            return s;
        } else {
            char[] cc = new char[length];
            int i = 0;
            for (; i < length - s.length(); i++) {
                cc[i] = '0';
            }
            for (int j=0; j < s.length();i++,j++) {
                // System.out.println(i);
                cc[i] = s.charAt(j);
            }
            return new String(cc);
        }
    }

    public static String comString2String(String s, int length) {
        if (s.length() > length) {
            throw new RuntimeException("length is too short.");
        } else if (s.length() == length) {
            return s;
        } else {
            char[] cc = new char[length];
            int i = 0;
            for (; i < length - s.length(); i++) {
                cc[i] = '0';
            }
            for (int j=0; j < s.length();i++,j++) {
                // System.out.println(i);
                cc[i] = s.charAt(j);
            }
            return new String(cc);
        }
    }

    /***
     * 功能描述:将double类型的时间转换为 正常的时间格式如: 2013-08-16 13:04:12 。精确度：精确到秒。
     * 此函数逻辑混乱,最好不要深入研究
     * ***/
    public static String getTimestampByDblTime(double dTime) {

        int alldays = (int) dTime, year = 0, mon = 0, day = 0, hour, minute, second;
        double allFloatPartDay = dTime - alldays;// double时间的小数部分
        int remainDays = alldays-1;
        StringBuffer timeStamp = new StringBuffer();
        timeStamp.setLength(0);

        // get year & mon
        Calendar c = Calendar.getInstance();
        for (int i = 1900; i < c.get(Calendar.YEAR)+1; i++) {
            if(remainDays <= getDaysByYear(i)){//remainDays<=当年的天数，那么year=i
                year = i;
                break;
            }
            remainDays -= getDaysByYear(i);
        }
        int[] days_each_month = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (isLeepYear(year)) {
            days_each_month[1] = 29;
        }
        //get day
        int days = 0;
        for (int i = 0; i < 12; i++) {
            if ((days + days_each_month[i]) >= remainDays) {
                mon = i + 1;
                day = remainDays - days;
                break;
            }
            days += days_each_month[i];
        }
        // get hour,minute,second
        hour = (int) (24 * allFloatPartDay);
        minute = (int) (24 * 60 * allFloatPartDay - 60 * (hour));
        second = (int) (24 * 60 * 60 * (allFloatPartDay) - 60 * minute - 60 * 60 * hour);

        timeStamp.append(year).append("-").append(comInt2String(checkTimeMaxVal(mon, "MON"), 2))
                .append("-").append(comInt2String(checkTimeMaxVal(day, "DAY"), 2)).append(" ")
                .append(comInt2String(checkTimeMaxVal(hour, "HOUR"), 2)).append(":")
                .append(comInt2String(checkTimeMaxVal(minute, "MINUTE"), 2)).append(":")
                .append(comInt2String(checkTimeMaxVal(second, "SECOND"), 2));
        return timeStamp.toString();
    }

    public static int checkTimeMaxVal(int time, String type) {
        if (("MON").equals(type) && time > 12) {
            return 0;
        }
        if (("DAY").equals(type) && time > 31) {
            return 0;
        }
        if (("HOUR").equals(type) || ("MINUTE").equals(type)
                || ("SECOND").equals(type)) {
            if (time >= 60) {
                return 0;
            }
        }
        return time;
    }

    /**
     * Get Date diff value (By day)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getDiffDay(String startTime, String endTime) {
        long diffDays = 0;
        Calendar c1 = Calendar.getInstance();
        c1.clear();
        Calendar c2 = Calendar.getInstance();
        c2.clear();
        Date start_Time = String2Date(startTime);
        Date end_Time = String2Date(endTime);

        // Set the date for both of the calendar instance
        c1.setTime(start_Time);
        c2.setTime(end_Time);

        // Get the represented date in milliseconds
        long time1 = c1.getTimeInMillis();
        long time2 = c2.getTimeInMillis();

        // Calculate difference in milliseconds
        long diff = time2 - time1;

        // Difference in days
        diffDays = diff / (24 * 60 * 60 * 1000);

        return diffDays;
    }

    /**
     * Get getDateTime yyyy-MM-dd
     *
     * @return
     */
    public static String getDateTime(String DateTime) {
        Date dateTime = String2Date(DateTime);
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
        String evt_timestamp = tempDate.format(dateTime);
        return evt_timestamp;
    }
    public static String getYesterData() {//获取当前日期的前一天日期
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        Date dBefore = calendar.getTime();   //得到前一天的时间
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
        String evt_timestamp = tempDate.format(dBefore);
        return evt_timestamp;
    }
    public static String getYesterData(String DateTime) {//获取指定日期的前一天日期
        Calendar calendar = Calendar.getInstance(); //得到日历
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = tempDate.parse(DateTime);
        } catch (ParseException ex) {
            // TODO Auto-generated catch block
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        String strDate = tempDate.format(calendar.getTime());
        return strDate;
    }

    public static String getTomorrowData(String DateTime) {//获取指定日期的后一天日期
        Calendar calendar = Calendar.getInstance(); //得到日历
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = tempDate.parse(DateTime);
        } catch (ParseException ex) {
            // TODO Auto-generated catch block
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);  //设置为前一天
        String strDate = tempDate.format(calendar.getTime());
        return strDate;
    }


    //	public static String getDataAfter(String DateTime ,Integer x) {//获取当前日期的后6个月日期
//		Calendar calendar = Calendar.getInstance(); //得到日历
//		calendar.add(Calendar.MONTH, +x);  //设置为后几个月日期
//		Date dBefore = calendar.getTime();   //得到后几个月的时间
//		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
//		String evt_timestamp = tempDate.format(dBefore);
//		return evt_timestamp;
//	}
    public static String getDateAfter(String DateTime,Integer x) {//获取指定日期的后x个月
        Calendar calendar = Calendar.getInstance(); //得到日历
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = tempDate.parse(DateTime);
        } catch (ParseException ex) {
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, +x);
        String strDate = tempDate.format(calendar.getTime());
        return strDate;
    }
    public static String getCurTime() {//获取当前日期的08整时间
        Calendar dateTime = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd 08:00:00");
        String evt_timestamp = tempDate.format(dateTime.getTime());
        return evt_timestamp;
    }

    public static String getYesterTime() {//获取当前日期的前一天的08整时间
//		Date dNow = new Date();   //当前时间
//		Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
//		calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        Date dBefore = calendar.getTime();   //得到前一天的时间
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd 08:00:00");
        String evt_timestamp = tempDate.format(dBefore);
        return evt_timestamp;
    }

    public static String getBefore4Time(String timestamp) {//获取当前时间往前推4小时的时间点
        Date dNow = String2Date(timestamp);
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.HOUR_OF_DAY, -4);  //设置为当前时间前推4小时
        Date dBefore = calendar.getTime();   //得到前推4小时的时间
        SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
        String evt_timestamp = tempDate.format(dBefore);
        return evt_timestamp;
    }

    public static String getThickFormat(Double thickness){//将小数0.6转换为0060的格式
        String S = thickness + "";
        String[] s1 = S.split("\\.");//以小数点分隔
        if(s1[0].length() == 1){
            s1[0] = "0" + s1[0];
        }
        if(s1[1].length() == 1){
            s1[1] = s1[1] + "0";
        }else{
            s1[1] = s1[1].substring(0,2);
        }
        return s1[0] + s1[1];
    }

    public static String getShiftType(){//根据系统时间，判断白夜班
        Calendar dateTime = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String cur_timestamp = tempDate.format(dateTime.getTime());
        SimpleDateFormat temp_8 = new SimpleDateFormat("yyyy-MM-dd 08:00:00");
        String timestamp_8 = temp_8.format(dateTime.getTime());
        SimpleDateFormat temp_20 = new SimpleDateFormat("yyyy-MM-dd 20:00:00");
        String timestamp_20 = temp_20.format(dateTime.getTime());
        try {
            Date curTime = String2Date(cur_timestamp);
            Date cur_8_time = String2Date(timestamp_8);
            Date cur_20_time = String2Date(timestamp_20);
            if (curTime.getTime() >= cur_8_time.getTime() &&
                    curTime.getTime() < cur_20_time.getTime()) {
                return "F";
            } else if (curTime.getTime() < cur_8_time.getTime() ||
                    curTime.getTime() >= cur_20_time.getTime()) {
                return "R";
            }
        } catch (Exception ex) {
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        return "F";
    }

    public static String  getLotData() {
        Calendar dateTime = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String cur_timestamp = tempDate.format(dateTime.getTime());
        SimpleDateFormat temp_0 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String timestamp_0 = temp_0.format(dateTime.getTime());
        SimpleDateFormat temp_8 = new SimpleDateFormat("yyyy-MM-dd 08:00:00");
        String timestamp_8 = temp_8.format(dateTime.getTime());
        try {
            Date evtTime = String2Date(cur_timestamp);
            Date evt_0_time = String2Date(timestamp_0);
            Date evt_8_time = String2Date(timestamp_8);
            if (evtTime.getTime() >= evt_0_time.getTime() &&
                    evtTime.getTime() < evt_8_time.getTime()) {
                dateTime.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
            }
        } catch (Exception ex) {
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        Date dBefore = dateTime.getTime();   //得到日期
        SimpleDateFormat evtDate = new SimpleDateFormat("yyyyMMdd");
        String evt_data = evtDate.format(dBefore);
        return evt_data;
    }

    public static Timestamp getCurrentTimestampToMilli() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String evt_timestamp = tempDate.format(now.getTime());
        Timestamp timestatmp = null;
        try {
            timestatmp = Timestamp.valueOf(evt_timestamp);
        } catch (Exception ex) {
            logger.error(XxdUtils.stackTraceToString(ex));
        }
        return timestatmp;
    }
    /**
     * Get time diff value (By millisecond)毫秒
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getDiffMillisec(Timestamp startTime, Timestamp endTime) {
        long diffMillisec = 0;
        diffMillisec = endTime.getTime() - startTime.getTime();
        return diffMillisec;
    }

    /**
     * 大于9的数字转换为字母
     */
    public static String I2S(Integer numb){
        String str = "0";
        switch(numb){
            case 1:
                str = "1";
                break;
            case 2:
                str = "2";
                break;
            case 3:
                str = "3";
                break;
            case 4:
                str = "4";
                break;
            case 5:
                str = "5";
                break;
            case 6:
                str = "6";
                break;
            case 7:
                str = "7";
                break;
            case 8:
                str = "8";
                break;
            case 9:
                str = "9";
                break;
            case 10:
                str = "A";
                break;
            case 11:
                str = "B";
                break;
            case 12:
                str = "C";
                break;
            case 13:
                str = "D";
                break;
            case 14:
                str = "E";
                break;
            case 15:
                str = "F";
                break;
            case 16:
                str = "G";
                break;
            case 17:
                str = "H";
                break;
            case 18:
                str = "I";
                break;
            case 19:
                str = "J";
                break;
            case 20:
                str = "K";
                break;
            case 21:
                str = "L";
                break;
            case 22:
                str = "M";
                break;
            case 23:
                str = "N";
                break;
            case 24:
                str = "O";
                break;
            case 25:
                str = "P";
                break;
            case 26:
                str = "Q";
                break;
            case 27:
                str = "R";
                break;
            case 28:
                str = "S";
                break;
            case 29:
                str = "T";
                break;
            case 30:
                str = "U";
                break;
            case 31:
                str = "V";
                break;
            case 32:
                str = "W";
                break;
            case 33:
                str = "X";
                break;
            case 34:
                str = "Y";
                break;
            case 35:
                str = "Z";
                break;
        }
        return str;
    }


    /**
     * @Description: 1-23 ---》 A-Z
     * @Date:16:16 2018/8/17
     */
    public static String I2S2(Integer numb){
        String str = "0";
        switch(numb){
            case 1:
                str = "A";
                break;
            case 2:
                str = "B";
                break;
            case 3:
                str = "C";
                break;
            case 4:
                str = "D";
                break;
            case 5:
                str = "E";
                break;
            case 6:
                str = "F";
                break;
            case 7:
                str = "G";
                break;
            case 8:
                str = "H";
                break;
            case 9:
                str = "I";
                break;
            case 10:
                str = "J";
                break;
            case 11:
                str = "K";
                break;
            case 12:
                str = "L";
                break;
            case 13:
                str = "M";
                break;
            case 14:
                str = "N";
                break;
            case 15:
                str = "O";
                break;
            case 16:
                str = "P";
                break;
            case 17:
                str = "Q";
                break;
            case 18:
                str = "R";
                break;
            case 19:
                str = "S";
                break;
            case 20:
                str = "T";
                break;
            case 21:
                str = "U";
                break;
            case 22:
                str = "V";
                break;
            case 23:
                str = "W";
                break;
            case 24:
                str = "X";
                break;
            case 25:
                str = "Y";
                break;
            case 26:
                str = "Z";
                break;
        }
        return str;
    }

    public static Integer S2I(String str){
        Integer numb = 0;
        switch(str.charAt(0)){
            case '1':
                numb = 1;
                break;
            case '2':
                numb = 2;
                break;
            case '3':
                numb = 3;
                break;
            case '4':
                numb = 4;
                break;
            case '5':
                numb = 5;
                break;
            case '6':
                numb = 6;
                break;
            case '7':
                numb = 7;
                break;
            case '8':
                numb = 8;
                break;
            case '9':
                numb = 9;
                break;
            case 'A':
                numb = 10;
                break;
            case 'B':
                numb = 11;
                break;
            case 'C':
                numb = 12;
                break;
            case 'D':
                numb = 13;
                break;
            case 'E':
                numb = 14;
                break;
            case 'F':
                numb = 15;
                break;
            case 'G':
                numb = 16;
                break;
            case 'H':
                numb = 17;
                break;
            case 'I':
                numb = 18;
                break;
            case 'J':
                numb = 19;
                break;
            case 'K':
                numb = 20;
                break;
            case 'L':
                numb = 21;
                break;
            case 'M':
                numb = 22;
                break;
            case 'N':
                numb = 23;
                break;
            case 'O':
                numb = 24;
                break;
            case 'P':
                numb = 25;
                break;
            case 'Q':
                numb = 26;
                break;
            case 'R':
                numb = 27;
                break;
            case 'S':
                numb = 28;
                break;
            case 'T':
                numb = 29;
                break;
            case 'U':
                numb = 30;
                break;
            case 'V':
                numb = 31;
                break;
            case 'W':
                numb = 32;
                break;
            case 'X':
                numb = 33;
                break;
            case 'Y':
                numb = 34;
                break;
            case 'Z':
                numb = 35;
                break;
        }
        return numb;
    }
    //随即生成16位字符串 by wfq
    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取当前时间(格式yyyy-MM-dd HH:mm:ss)
     * @return String currentDateTime
     */
    public static String getCurrentDateTime() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateTime = tempDateTime.format(now.getTime());
        return currentDateTime;
    }
    /**
     * 获取当前时间(格式yyyy-MM-dd)
     * @return String currentDateTime
     */
    public static String getCurrentDateTime2() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat tempDateTime = new SimpleDateFormat("yyyy-MM-dd");
        String currentDateTime = tempDateTime.format(now.getTime());
        return currentDateTime;
    }

    /**
     * 获取当前日期(格式yyyy-MM-dd)
     * @return String currentDate
     */
    public static String getCurrentDate(String DateTime) {
        Date dateTime = String2Date(DateTime);
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = tempDate.format(dateTime);
        return currentDate;
    }

    //+1s by wfq
    public static Timestamp sec(Timestamp firstTime){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date begdate = new Date(firstTime.getTime());
        calendar.setTime(begdate);
        calendar.add(Calendar.SECOND, +1);
        String finalTime = tempDate.format(calendar.getTime());
        return Timestamp.valueOf(finalTime);
    }

    /**
     *
     * @param i 年份截取末位个数
     * @param flg Y for 日期转换,N for 日期不转换,其他 for 不输出日期
     * @return
     */
    public static String getYearMonthDayEight(Integer i, String flg){
        String yearMonthDay = "***";
        String tempYearMonthDay = getLotData();
        yearMonthDay = tempYearMonthDay.substring(4-i, 4);
        //月份需要转换（1~9艺术字体现，10~12以字母A~C体现）
        yearMonthDay = yearMonthDay + Digit[Integer.valueOf(tempYearMonthDay.substring(4,6))];
        if("Y".equals(flg)){//日期需要转换（1~9用数字，10~31用字母，排除I和O）
            yearMonthDay = yearMonthDay + Digit[Integer.valueOf(tempYearMonthDay.substring(6,8))];
        }else if("N".equals(flg)){
            yearMonthDay = yearMonthDay + tempYearMonthDay.substring(6,8);
        }
        return yearMonthDay;
    }

    public static String getYearMonthDayZero(Integer i, String flg){
        String yearMonthDay = "***";
        Calendar dateTime = Calendar.getInstance();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
        String tempYearMonthDay = tempDate.format(dateTime.getTime());
        yearMonthDay = tempYearMonthDay.substring(4-i, 4);
        //月份需要转换（1~9艺术字体现，10~12以字母A~C体现）
        yearMonthDay = yearMonthDay + Digit[Integer.valueOf(tempYearMonthDay.substring(4,6))];
        if("Y".equals(flg)){//日期需要转换（1~9用数字，10~31用字母，排除I和O）
            yearMonthDay = yearMonthDay + Digit[Integer.valueOf(tempYearMonthDay.substring(6,8))];
        }else if("N".equals(flg)){
            yearMonthDay = yearMonthDay + tempYearMonthDay.substring(6,8);
        }
        return yearMonthDay;
    }

    /**
     * 0~1，A~Z(I和O除外)
     * 将10进制数转换为数字和字母组合的字符串
     * @return
     */
    public static String getSerialNo(Integer iSerial, Integer i){
        String sSerial = "*";
        if(i == 1 && iSerial<= 33){
            sSerial = String.valueOf(Digit[iSerial]);
        }else if(i == 2 && iSerial<= 1155){//33*34+33
            sSerial = String.valueOf(Digit[iSerial/34])
                    + String.valueOf(Digit[iSerial%34]);
        }else if(i == 3 && iSerial<= 39303){//33*34*34+33*34+33
            sSerial = String.valueOf(Digit[iSerial/34/34])
                    + String.valueOf(Digit[iSerial/34%34])
                    + String.valueOf(Digit[iSerial%34%34]);
        }
        return sSerial;
    }

    /**
     * 获取thickness type
     */
    public static String getThicknessTyp(String thickness){
        String thicknessTyp = "*";
        Map<String,String> thick = new HashMap<String,String>();
        thick.put("0.4", "A");
        thick.put("0.5", "B");
        thick.put("0.6", "C");
        thick.put("0.63", "D");
        thick.put("0.7", "E");
        thick.put("1.1", "F");
        thick.put("0.2", "G");
        thick.put("0.3", "H");

        thick.put("0.25", "J");
        thick.put("0.225", "K");
        thick.put("0.63+0.7", "L");
        thick.put("0.215", "M");
        thick.put("0.265", "N");
        thick.put("0.1", "P");
        thick.put("0.15", "Q");
        thick.put("0.2+0.1", "S");

        thick.put("0.18", "T");
        thick.put("0.5+0.4", "R");
        thick.put("0.2+0.4", "U");

        if(thick.get(thickness) != null){
            thicknessTyp = thick.get(thickness);
        }
        return thicknessTyp;
    }

    /**
     * 获取月份代码
     * 区分公元单双数
     */
    public static String getMonthTyp(Calendar rightNow){
        String strMonth = "*";
        char[] Digit_E = { '*', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'}; //双数公元年
        char[] Digit_O = { '*', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q'}; //单数公元年
        if(rightNow.get(Calendar.YEAR)%2 == 0){//双数公元年
            strMonth =  String.valueOf(Digit_E[rightNow.get(Calendar.MONTH) + 1]);
        }else{//单数公元年
            strMonth =  String.valueOf(Digit_O[rightNow.get(Calendar.MONTH) + 1]);
        }
        return strMonth;
    }
    /****
     * 测试main函数,获取double类型时间，后再转换成String 时间
     * ***/
    public static void main(String[] args) {
        double dTime = getDoubleTime();
        System.out.println("" + getDoubleTime() + "\n");
        // getTimestampByDblTime(41574.10797572048);
        getTimestampByDblTime(dTime);
        System.out.println("" + getTimestampByDblTime(42369.65204627315) + "\n");

    }

    //判断保养是否过期
    public static boolean isDealy(String dataString){
        boolean is_delay = false;
        Date data = String2Date(dataString);
        Calendar calendar = Calendar.getInstance(); //得到日历
        Date d2 = calendar.getTime();
        if(d2.getTime() > data.getTime()){
            is_delay = true;
        }
        return is_delay;
    }

    //String -> Data
    public static Date getYearDateAfter(String dataString, Integer x, Integer i, String strMySQLTimeFormat){
        Calendar calendar = Calendar.getInstance(); //得到日历
        Date data = String2Date(dataString);
        calendar.setTime(data);
        calendar.add(Calendar.YEAR, +x*i);
        Date dateAfter = calendar.getTime();
        SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
        return String2Date(tempDate.format(dateAfter));
    }

    //获取指定日期的后x个月
    public static Date getMonthDateAfter(String dataString, Integer x, Integer i, String strMySQLTimeFormat){
        Calendar calendar = Calendar.getInstance(); //得到日历
        Date data = String2Date(dataString);
        calendar.setTime(data);
        calendar.add(Calendar.MONTH, +x*i);
        Date dateAfter = calendar.getTime();
        SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
        return String2Date(tempDate.format(dateAfter));
    }

    //获取指定日期的后x星期
    public static Date getWeekOfYearDateAfter(String dataString, Integer x, Integer i, String strMySQLTimeFormat){
        Calendar calendar = Calendar.getInstance(); //得到日历
        Date data = String2Date(dataString);
        calendar.setTime(data);
        calendar.add(Calendar.WEEK_OF_YEAR, +x*i);
        Date dateAfter = calendar.getTime();
        SimpleDateFormat tempDate = new SimpleDateFormat(strMySQLTimeFormat);
        return String2Date(tempDate.format(dateAfter));
    }

    public static String Date2String(Date date, String strMySQLTimeFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(strMySQLTimeFormat);
        return sdf.format(date);
    }

    /**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    //日班
    public static boolean between8To20(Date nowTime) throws Exception{
        Calendar dateTime = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd 08:00:00");
        String today8 = tempDate.format(dateTime.getTime());
        Date today8date = formatter.parse(today8);

        SimpleDateFormat tempDate2 = new SimpleDateFormat("yyyy-MM-dd 20:00:00");
        String today20 = tempDate2.format(dateTime.getTime());
        Date today20date = formatter.parse(today20);

        if (nowTime.after(today8date) && nowTime.before(today20date)) {
            return true;
        } else {
            return false;
        }

    }

    //夜班
    public static boolean between20To8(Date nowTime) throws Exception{
        Calendar dateTime = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        SimpleDateFormat tempDate2 = new SimpleDateFormat("yyyy-MM-dd 20:00:00");
        String today20 = tempDate2.format(dateTime.getTime());
        Date today20date = formatter.parse(today20);

        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.add(Calendar.DAY_OF_MONTH, +1);  //设置为明天
        Date tomorry = calendar.getTime();   //得到明天的时间
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd 08:00:00");
        String tomorryDate8 = tempDate.format(tomorry);
        Date tomorrydate8 = formatter.parse(tomorryDate8);

        if (nowTime.after(today20date) && nowTime.before(tomorrydate8)) {
            return true;
        } else {
            return false;
        }

    }

    //获得前几天的时间
    public static Date getDateBefore(Date d,int day){
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    public static String getYearMonthDay(int dayDiff) {
        LocalDate localDate = LocalDate.now();

        String year = String.valueOf(localDate.getYear());
        String month = String.format("%02d", localDate.getMonthValue());
        String day = "";
        if(dayDiff ==0){
            day = String.format("%02d", localDate.getDayOfMonth());
        }else{
            day = String.format("%02d", localDate.getDayOfMonth()+ dayDiff);
        }
        return year + "-" + month + "-" + day;

    }

    public static String getYearMonthDay(boolean dayFlg) {
        LocalDate localDate = LocalDate.now();
        String year = String.valueOf(localDate.getYear());
        String month = String.format("%02d", localDate.getMonthValue());
        String day = String.format("%02d", localDate.getDayOfMonth());
        String shortYear = year.substring(2, 4);
        if (dayFlg) {
            return shortYear + month + day;
        }
        return shortYear + month;
    }
}
